import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";

const { getCooperationBusiness } = require("@src/api/member");
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: mall,
      fields: ["curLocation", "storeId"],
      actions: ["initLocation", "setAddress", "setStoreId"],
    },
  ],
  properties: {},
  data: {
    shopList: [],
  },
  methods: {
    onLoad() {
      // 定位
      this.initLocation()
        .then((res) => {
          let params = { latitude: res.latitude, longitude: res.longitude };
          this.getList(params);
        })
        .catch((error) => {
          this.getList({});
        });
    },
    async getList(params) {
      const res = await getCooperationBusiness({ ...params });
      if (res) {
        res?.data?.map((item) => {
          if (item.distance) {
            let distance = item.distance;
            if (distance < 1000) {
              distance = distance.toFixed(2) + "m";
            } else {
              distance = (Math.round(distance / 100) / 10).toFixed(1) + "km";
            }
            item.distance = distance;
          }
        });
        this.setData({
          shopList: res?.data,
        });
      }
    },
    handleOpenMap(e) {
      let data = e.currentTarget.dataset.item;
      console.log("打开", data);
      wx.openLocation({
        latitude: data.latitude,
        longitude: data.longitude,
        // name: data.name,
        address: data.address,
      });
    },
  },
});
